package com.framework.config;


import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;

public class BigDecimalSerializer extends JsonSerializer<BigDecimal> {
//    @Override
//    public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
//        gen.writeString(value.stripTrailingZeros().toPlainString());
//    }

    public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) {
        if (null != bigDecimal ) {
            String plainString = bigDecimal.stripTrailingZeros().toPlainString();
            try {
                jsonGenerator.writeString(plainString);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } else {
            try {
                jsonGenerator.writeString(BigDecimal.ZERO.toPlainString());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}